/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication1;

import java.util.Iterator;
import java.util.LinkedList;


/**
 *
 * @author Oren
 */
public class Programs {
     private LinkedList<RunProgram> list ;
     private LinkedList<Thread> list2;
     private int time ;
   

     public Programs()
     {
         list = new LinkedList<RunProgram>();
         list2 = new LinkedList<Thread>();
         time = 0; 
     }
     public void add (RunProgram rp)
     {
         list.add(rp);
     }
     public boolean isEmpty()
     {
         return list.isEmpty();
     }
     public void remove (RunProgram rp)
     {
         list.remove(rp);
     }

     public void run(int time) throws InterruptedException
     {

         this.time = time ;
         // TODO
         // running multiple threads (all the thread are in the linkedlist)
         //Executor executor = Executors.newCachedThreadPool();
         // initiate all RunPrograms
         RunProgram temp ;
         Thread t ;
         Iterator<RunProgram> it ;
         Iterator<Thread> it2 ;
         it = list.iterator();
         while (true)
         {
            temp = it.next();
            temp.setTime(time);
            t = new Thread(temp);
            t.start();
            list2.add(t);
            if (!it.hasNext())
                break;
            //executor.execute (it.next());
            //((ExecutorService) executor).shutdown();
         }
         // not good Thread.sleep(time*1000);
        
         it2 = list2.iterator();
         while (true)
         {
            it2.next().join();
           
            if (!it2.hasNext())
                break;
            //executor.execute (it.next());
            //((ExecutorService) executor).shutdown();
         }
     }

}
